package com.cat.springbootdemoidea.controller;


import com.cat.springbootdemoidea.entity.Product;
import com.cat.springbootdemoidea.entity.User;
import com.cat.springbootdemoidea.mapper.UserMapper;
import com.cat.springbootdemoidea.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zss
 * @since 2020-09-16
 */
@RestController
@RequestMapping("/user")
public class UserController {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private IUserService iUserService;

    @Autowired
    private UserMapper userMapper;
    /**
     * 测试mybatis-plus的service层
     * @param id
     * @return
     */
    @RequestMapping("/getUserNameByServicePlus")
    public String getUserNameByServicePlus(String id){

        User user = iUserService.getById(id);
        if(user!=null){
            System.out.println(user.toString());
            logger.info(user.toString());
            logger.info("这是logger！");
            return user.getUserName();
        }
        return "user is empity!";
    }

    /**
     * 测试mybatis-plus的service层
     * @param userName
     * @param address
     */
    @RequestMapping("/saveUserByServicePlus")
    public void saveUserByServicePlus(String userName,String address){
        User user = new User();
        user.setAddress(address);
        user.setUserName(userName);
        iUserService.save(user);
    }

    /**
     * 测试mybatis-plus的mapper层
     * @param userName
     * @param address
     */
    @RequestMapping("/saveUserByMapperPlus")
    public void saveUserByMapperPlus(String userName,String address){
        User user = new User();
        user.setAddress(address);
        user.setUserName(userName);
        userMapper.insert(user);
    }

    /**
     * 测试mybatis-plus的mapper层
     * @param id
     * @return
     */
    @RequestMapping("/getUserByMapperPlus")
    public  User getUserByMapperPlus(String id){
        User user = userMapper.selectById(id);
        if(user!=null){
            System.out.println(user.toString());
            return user;
        }
        return null;
    }


}
